package net.xiduth.game.player.content;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import net.xiduth.game.World;
import net.xiduth.game.player.Player;
import net.xiduth.utils.Utils;

/**
 * 
 * @author Plato
 * 
 */

public class ReportAbuse {

	public static void openReport(Player player, String offender) {
		if (player.getAttackedByDelay() + 10000 > Utils.currentTimeMillis()) {
			player.getPackets().sendGameMessage(
					"You cannot do that while under attack!");
			return;
		}
		if (player.getInterfaceManager().containsScreenInter()) {
			player.getPackets()
					.sendGameMessage(
							"Please finish what you're doing before opening Report Abuse.");
			return;
		}
		player.getInterfaceManager().sendInterface(594);
	}

	public static void handleReport(Player player, String username, int ruleId,
			boolean mute) {
		Player offender;
		offender = World.getPlayerByDisplayName(username);
		player.sm("Thank-you, your abuse report has been received.");
		logReport(player, username, ruleId);
		if (mute)
			offender.setMuted(Utils.currentTimeMillis() + 48 * 60 * 60 * 1000);
	}

	private static void logReport(Player player, String offender, int offense) {
		try {
			BufferedWriter bf = new BufferedWriter(new FileWriter(
					"data/playersaves/reporting/reports.txt", true));
			bf.write("[" + DateFormat.getDateTimeInstance().format(new Date())
					+ " "
					+ Calendar.getInstance().getTimeZone().getDisplayName()
					+ "] "
					+ Utils.formatPlayerNameForDisplay(player.getUsername())
					+ " has reported: " + offender + " for: "
					+ getOffense(offense));
			System.err.println("["
					+ DateFormat.getDateTimeInstance().format(new Date()) + " "
					+ Calendar.getInstance().getTimeZone().getDisplayName()
					+ "] "
					+ Utils.formatPlayerNameForDisplay(player.getUsername())
					+ " has reported a " + offender + ". ");
			bf.newLine();
			bf.flush();
			bf.close();
		} catch (IOException ignored) {
		}
	}

	public static String getOffense(int ruleId) {
		String ruleName = "";
		switch (ruleId) {
		case 6:
			return "Buying or selling account";
		case 9:
			return "Encouraging rule breaking";
		case 5:
			return "Staff impersonation";
		case 7:
			return "Macroing/use of bots";
		case 15:
			return "Scamming";
		case 4:
			return "Exploiting a bug";
		case 16:
			return "Seriously offensive language";
		case 17:
			return "Solicitation";
		case 18:
			return "Disruptive behaviour";
		case 19:
			return "Offensive account name";
		case 20:
			return "Real life threats";
		case 13:
			return "Asking for real life info";
		case 21:
			return "Breaking real world laws";
		case 11:
			return "Advertising websites";
		}
		return ruleName;
	}

}